local ziyouzhinu = fk.CreateSkill {
    name = "ziyouzhinu",
    tags = { Skill.Quest },
}
Fk:loadTranslationTable{
  ["ziyouzhinu"] = "自由之怒",
  [":ziyouzhinu"] = "使命技，当你受到1点伤害后，你可以翻面并摸两张牌。<br>\
  ⬤　成功：若此时你的体力值为3且你背面朝上，则你减1点体力上限并翻面，然后获得技能“真·星盾打击”，若你的没有【振金盾牌】，你获得之。",
  ["#ziyouzhinu_invoke"] = "自由之怒：你可翻面并摸两张牌。",  
}

ziyouzhinu:addEffect(fk.Damaged, {
    trigger_times = function(self, event, target, player, data)
        return data.damage
      end,
    can_trigger = function(self, event, target, player, data)
      return target == player and player:hasSkill(ziyouzhinu.name) and
        not player:getQuestSkillState(ziyouzhinu.name)
    end,
    
    on_cost = function(self, event, target, player, data)
        return player.room:askToSkillInvoke(player, {
          skill_name = ziyouzhinu.name,
          data = nil,
          prompt = "#ziyouzhinu_invoke"
        })
      end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      room:drawCards(player,2)
      player:turnOver()
      if player.hp == 3 and not player.faceup then 
        room:updateQuestSkillState(player, ziyouzhinu.name, false)
        room:changeMaxHp(player,-1)
        player:turnOver()
        room:handleAddLoseSkills(player,"zhenxdj",nil)
        local has_hammer = false
        for _, id in ipairs(player:getCardIds{Player.Hand, Player.Equip, Player.Judge}) do
            if Fk:getCardById(id).name == "progoldsheild" then
                has_hammer = true
                break
            end
        end
        if not has_hammer then
            local allCardNames = {}
            for _, p in ipairs(room.players) do
                for _, id in ipairs(p:getCardIds("hej&he")) do
                    local card = Fk:getCardById(id)
                    if card.name == "progoldsheild" then
                        table.insert(allCardNames, card)
                        break
                    end
                end
            end
            if #allCardNames == 0 then 
                for _, id in ipairs(room.draw_pile) do
                    local card = Fk:getCardById(id)
                    if card.name == "progoldsheild" then
                        table.insert(allCardNames, card)
                        break
                    end
                end
                if #allCardNames == 0 then 
                    for _, id in ipairs(room.discard_pile) do
                        local card = Fk:getCardById(id)
                        if card.name == "progoldsheild" then
                            table.insert(allCardNames, card)
                            break
                        end
                    end
                    if #allCardNames == 0 then 
                        for _, id in ipairs(room.processing_area) do
                            local card = Fk:getCardById(id)
                            if card.name == "progoldsheild" then
                                table.insert(allCardNames, card)
                                break
                            end
                        end
                        if #allCardNames == 0 then 
                            for _, id in ipairs(room.void) do
                                local card = Fk:getCardById(id)
                                if card.name == "progoldsheild" then
                                    table.insert(allCardNames, card)
                                    break
                                end
                            end
                        end
                    end
                end
            end
            if #allCardNames ~= 0 then 
                local dummy = Fk:cloneCard("dilu")
                dummy:addSubcards(allCardNames)
                room:obtainCard(player, dummy, true, fk.ReasonPrey, player, "ziyougz")
            end
        end
        room:invalidateSkill(player, ziyouzhinu.name)

      end
    end,
  })

return ziyouzhinu 